# 交叉熵损失 参数详解

import torch
import torch.nn

# 4个样本 3个类别
logits = torch.tensor([[1, 2, 3],
                      [4, 5, 6],
                      [2, 4, 7],
                      [3, 5, 6]], dtype=torch.float32)
# 标签
target = torch.tensor([0, 1, 2, 1])


# 1 标准损失
criterion = torch.nn.CrossEntropyLoss()
loss = criterion(logits, target)
print(loss)
print("-"*20)

# 2 使用weight参数
weight = torch.tensor([1, 10, 1], dtype=torch.float32)
criterion = torch.nn.CrossEntropyLoss(weight=weight)
loss = criterion(logits, target)
print(loss)
print("-"*20)

# 3 使用 ignore_index

# 4 使用 label_smoothing

# 5 使用 reduction
criterion = torch.nn.CrossEntropyLoss(reduction="sum")  # mean  sum
loss = criterion(logits, target)
print(loss)
print("-"*20)
